//
//  NextViewController.swift
//  CTPhotoBrowser
//
//  Created by Andney Jack on 2021/2/25.
//

import Kingfisher
import SnapKit
import UIKit

/// 随机颜色
@inlinable public func RandomColor() -> UIColor {
    let r = CGFloat(arc4random() % 256) / 255.0
    let g = CGFloat(arc4random() % 256) / 255.0
    let b = CGFloat(arc4random() % 256) / 255.0
    return UIColor(red: r, green: g, blue: b, alpha: 1.0)
}

@inlinable public func RandomImgUrl() -> String {
    return "https://culturecloud.oss-cn-hangzhou.aliyuncs.com/image/wallpaper/img_\(arc4random() % 109 + 1).jpg"
}

class MYItemView: UIImageView, CTCycleScrollItemViewProtocol {
    public var isShowed: Bool
    public var didFetchImage: Bool = false
    public var currentImage: UIImage? {
        return image
    }

    public let titleLabel = UILabel()

    override init(frame: CGRect) {
        isShowed = false
        super.init(frame: frame)

        contentMode = .scaleAspectFill
        layer.masksToBounds = true

        titleLabel.textColor = .red
        titleLabel.numberOfLines = 0
        titleLabel.font = .systemFont(ofSize: 22)
        addSubview(titleLabel)

        titleLabel.snp.makeConstraints { make in
            make.center.equalTo(self)
        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class NextViewController: UIViewController {
    weak var m_bannerView: CTBaseCycleScrollView<MYItemView>?

    deinit {
        print("\(self) 被释放了 .......")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white

        loadUI()
    }

    private func loadUI() {
        let bannerView = CTBaseCycleScrollView<MYItemView>(frame: .zero)
        bannerView.backgroundColor = .clear
        bannerView.autoScrollTimeInterval = 6
        bannerView.imageLoadHandler = { itemView, imgObj, index, placeholder in
            
            
            if let path = imgObj as? String {
                if path.hasPrefix("http") {
                    itemView.kf.setImage(with: URL(string: path), placeholder: placeholder)
                } else {
                    if let img = UIImage(named: path) {
                        itemView.image = img
                    } else if let img = UIImage(contentsOfFile: path) {
                        itemView.image = img
                    } else {
                        itemView.image = placeholder
                    }
                }
            } else if let url = imgObj as? URL {
                itemView.kf.setImage(with: url, placeholder: placeholder)
            } else if let image = imgObj as? UIImage {
                itemView.image = image
            } else {
                itemView.image = placeholder
            }

            itemView.titleLabel.text = "第\(index + 1)个"
        }
        view.addSubview(bannerView)
        m_bannerView = bannerView

        bannerView.snp.makeConstraints { make in
            make.center.equalTo(view)
            make.height.equalTo(260)
            make.width.equalTo(view).multipliedBy(0.95)
        }

        var imgList = [String]()
        for _ in 0 ..< 20 {
            imgList.append(RandomImgUrl())
        }

        bannerView.updateImageList(imgList)

        let preBtn = UIButton(frame: .zero)
        preBtn.tag = 1
        preBtn.setTitle("上一个", for: .normal)
        preBtn.backgroundColor = RandomColor()
        preBtn.addTarget(self, action: #selector(buttonClick(_:)), for: .touchUpInside)
        view.addSubview(preBtn)

        let nextBtn = UIButton(frame: .zero)
        nextBtn.tag = 2
        nextBtn.setTitle("下一个", for: .normal)
        nextBtn.backgroundColor = RandomColor()
        nextBtn.addTarget(self, action: #selector(buttonClick(_:)), for: .touchUpInside)
        view.addSubview(nextBtn)

        preBtn.snp.makeConstraints { make in
            make.bottom.equalTo(bannerView.snp.top).offset(-20)
            make.width.equalTo(100)
            make.height.equalTo(44)
            make.right.equalTo(view.snp.centerX).offset(-50)
        }

        nextBtn.snp.makeConstraints { make in
            make.width.height.bottom.equalTo(preBtn)
            make.left.equalTo(view.snp.centerX).offset(50)
        }
    }

    @objc func buttonClick(_ sender: UIButton) {
        if sender.tag == 1 {
            m_bannerView?.goBack(animated: true)
        } else if sender.tag == 2 {
            dismiss(animated: true, completion: nil)
//            m_bannerView?.goForward(animated: true)
        }
    }
}
